<!-- components/custom-payment/custom-payment.vue -->
<script lang="ts" setup>
  //  cv 支付方式组件
  import { orderPayApi } from '@/api/consult'
  import { ref, watch } from 'vue'

  // 在线支付弹层
  const paymentPopup = ref<UniHelper.UniPopupInstance>()

  // 打开弹层
  const open = () => {
    paymentPopup.value?.open!()
  }
  // 关闭弹层
  const close = () => {
    paymentPopup.value?.close!()
  }

  // 开放关闭和显示弹层的方法
  defineExpose({ open, close })

  // 接收两个父传子参数
  interface Props {
    /** 支付金额 */
    amount: number
    // 3.接收父传子参数
    show : boolean
  }
const props = defineProps<Props>()

  watch(
    // 4. 监听数据
    () => props.show,
    (newVal) => {
      if (newVal === true) {
        paymentPopup.value?.open!()
      } else {
        paymentPopup.value?.close!()
      }
    }
  )

  //  点谁谁高亮
  const activeIndex = ref(0)

  // 支付方式
  const paymentChannel = [
    {
      title: '微信支付',
      thumb: '/static/images/wechatpay-icon.png',
    },
    {
      title: '支付宝支付',
      thumb: '/static/images/alipay-icon.png',
    },
  ]


  // 1. 定义子传父
  interface Emits {
    (e: 'onCancel'): void
    // 绑定立即支付事件
    (e:'onConfirm',activeIndex:number): void
  }
  const emit = defineEmits<Emits>()
</script>
<template>
  <uni-popup :is-mask-click="false" ref="paymentPopup" type="bottom">
    <view class="payment-container">
      <view class="payment-header">
        <text class="title">选择支付方式</text>
        <uni-icons
          class="uni-icons-close"
          size="18"
          color="#333"
          type="closeempty"
          @click="emit('onCancel')"
        />
      </view>
      <!--  插值显示金额 -->
      <view class="order-amount">¥ {{ amount.toFixed(2) }} </view>
      <uni-list :border="false">
        <!--  v-for循环 -->
        <uni-list-item
          v-for="(item, index) in paymentChannel"
          :title="item.title"
          :thumb="item.thumb"
          @click="activeIndex = index"
          clickable
        >
          <template #footer>
            <uni-icons
              v-if="index === activeIndex"
              size="26"
              color="#16C2A3"
              type="checkbox-filled"
            />
            <uni-icons v-else size="26" color="#d1d1d1" type="circle" />
          </template>
        </uni-list-item>
      </uni-list>
      <!--  绑定点击事件 -->
      <button class="uni-button" @click="emit('onConfirm',activeIndex)">立即支付</button>
    </view>
  </uni-popup>
</template>

<style lang="scss">
  .payment-container {
    min-height: 400rpx;
    border-radius: 30rpx 30rpx 0 0;
    background-color: #fff;
    padding: 10rpx 30rpx 40rpx;

    .payment-header {
      height: 88rpx;
      line-height: 88rpx;
      text-align: center;
      margin-bottom: 20rpx;
      font-size: 32rpx;
      color: #333;
      position: relative;
    }

    .uni-icons-close {
      position: absolute;
      top: 2rpx;
      right: 0;
    }

    .order-amount {
      padding: 10rpx 0 10rpx;
      text-align: center;
      font-size: 40rpx;
      color: #333;
    }

    :deep(.uni-list-item__container) {
      padding: 40rpx 0 !important;
    }

    :deep(.uni-list-item--hover) {
      background-color: #fff !important;
    }

    :deep(.uni-list-item__icon) {
      margin-right: 0;
    }

    .uni-button {
      margin-top: 40rpx;
    }
  }
</style>
